package com.hey.amqpdemo.config;

import com.hey.amqpdemo.listener.ConfirmCallBackListener;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;

/**
 * @ClassName RabbitConfig
 * @Author hey
 * @Date 2019/5/9 13:40
 **/
@Configuration
public class RabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    ConfirmCallBackListener confirmCallBackListener;

    @PostConstruct
    public RabbitTemplate initTemplate(){
        rabbitTemplate.setConfirmCallback(confirmCallBackListener);
        return rabbitTemplate;
    }



    @Bean
    public Queue payQueue(){
        HashMap hashMap = new HashMap();
        hashMap.put("x-dead-letter-exchange","deadExchange");
        hashMap.put("x-dead-letter-routing-key","dead.hey");
        hashMap.put("x-message-ttl",3000);
        return new Queue("pay1Queue",true,false,false,hashMap);
    }

//    @Bean
//    public Queue delayQueue(){
//        return new Queue("delayQueue",true,false,false,hashMap);
//    }

    @Bean
    public TopicExchange exchange(){
        return new TopicExchange("pay1Exchange",true,false);
    }

    @Bean
    public TopicExchange deadExchange(){
        return new TopicExchange("deadExchange",true,false);
    }

    @Bean
    public Queue deadQueue(){
        return new Queue("deadQueue",true);
    }

    @Bean
    public Binding deadBinding(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("dead.*");
    }

    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(payQueue()).to(exchange()).with("pay1.*");
    }
}
